/**
 * jims
 */
package com.jims.master.modules.operation.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.operation.entity.Icd9ItemClass;
import com.jims.master.modules.operation.service.Icd9ItemClassService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * icd9编码对照Controller
 * @author zhuq
 * @version 2017-05-12
 */
@Controller
@RequestMapping(value = "${adminPath}/operation/icd9ItemClass")
public class Icd9ItemClassController extends BaseController {

	@Autowired
	private Icd9ItemClassService icd9ItemClassService;
	
	@ModelAttribute
	public Icd9ItemClass get(@RequestParam(required=false) String id) {
		Icd9ItemClass entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = icd9ItemClassService.get(id);
		}
		if (entity == null){
			entity = new Icd9ItemClass();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(Icd9ItemClass icd9ItemClass, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Icd9ItemClass> page = icd9ItemClassService.findPage(new Page<Icd9ItemClass>(request, response), icd9ItemClass); 
		model.addAttribute("page", page);
		return "modules/operation/icd9ItemClassIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(Icd9ItemClass icd9ItemClass, Model model) {
		model.addAttribute("icd9ItemClass", icd9ItemClass);
		return "modules/operation/icd9ItemClassForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(Icd9ItemClass icd9ItemClass, Model model) {
	    StringData data=new StringData();
		int i=icd9ItemClassService.save(icd9ItemClass);
        if(i>0){
            data.setCode("success");
            data.setData("保存icd9编码成功");
        }else{
            data.setCode("error");
            data.setData("保存icd9编码失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(Icd9ItemClass icd9ItemClass) {
	    StringData data=new StringData();
		int i=icd9ItemClassService.delete(icd9ItemClass);
        if(i>0){
            data.setCode("success");
            data.setData("删除icd9编码成功");
        }else{
            data.setCode("error");
            data.setData("删除icd9编码失败");
        }
		return data;
	}

    /**
     * @param icd9ItemClass,q 传递参数
     * @return List<icd9ItemClass>    返回类型
     * @throws
     * @Title: autoComplete
     * @author ZhuQ
     * @date 2017-05-12
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoComplete", ""})
    @ResponseBody
    public List<Icd9ItemClass> autoComplete(String q, String limit,Icd9ItemClass icd9ItemClass) {
        if(q!=null&&!q.equals("")){
            try {
                q = URLDecoder.decode(q, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<Icd9ItemClass> list = icd9ItemClassService.findAutoCompleteList(icd9ItemClass,q,limit);
        return list;
    }

}